﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Member_SelectCharacter : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        CharacterListBox.DataSource = UserData.Characters;
        CharacterListBox.DataTextField = "Name";
        CharacterListBox.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
         }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (CharacterListBox.Items.Count != UserData.Characters.Count || CharacterListBox.SelectedItem != null && CharacterListBox.SelectedValue != UserData.Characters[CharacterListBox.SelectedIndex].Name)
        {
            var name = CharacterListBox.SelectedValue;
            CharacterListBox.DataSource = UserData.Characters;
            CharacterListBox.DataTextField = "Name";
            CharacterListBox.DataBind();
            for (int i = 0; i < CharacterListBox.Items.Count; i++)
            {
                if (CharacterListBox.SelectedValue == name)
                    CharacterListBox.SelectedIndex = i;
            }
        }
        CharackterView.Character = CharacterListBox.SelectedIndex == -1 ? null : UserData.Characters[CharacterListBox.SelectedIndex];
  
        SelectCharacterButton.Enabled = CharacterListBox.SelectedIndex != -1;
    }

    protected void CharacterListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        CharackterView.Character = CharacterListBox.SelectedIndex == -1 ? null : UserData.Characters[CharacterListBox.SelectedIndex];
 
    }
    protected void SelectCharacterButton_Click(object sender, EventArgs e)
    {
        UserData.BeginEdit(CharacterListBox.SelectedIndex);
    }
    protected void DeleteCharacterButton_Click(object sender, EventArgs e)
    {
        if (CharacterListBox.SelectedIndex != -1)
            UserData.RemoveCharacter(CharacterListBox.SelectedIndex);
    }
}